/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package movetransfiles.move.translation;

import it.sauronsoftware.ftp4j.FTPAbortedException;
import it.sauronsoftware.ftp4j.FTPClient;
import it.sauronsoftware.ftp4j.FTPDataTransferException;
import it.sauronsoftware.ftp4j.FTPException;
import it.sauronsoftware.ftp4j.FTPFile;
import it.sauronsoftware.ftp4j.FTPIllegalReplyException;
import it.sauronsoftware.ftp4j.FTPListParseException;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import movetransfiles.ftp.FTPUtil;

/**
 *
 * @author srkrishnan
 */
public class Move {

    static String srcBase = "/quicksharkpro/";
    static String destBase = "/touparsix/";
    static String srcAdmin = "adminjoyb";
    static String destAdmin = "/touparsix/";

    public static void move() {
        FTPClient srcclient = FTPUtil.connect("ftp.joyhandbag.com", "ftpfortool@joyhandbag.com", "ftpfortool1!");
        FTPClient destclient = FTPUtil.connect("ftp.paradisdesrondes.com", "ftpfortool@paradisdesrondes.com", "ftpfortool1!");

        doCopy(srcclient, destclient);
    }

    private static void doCopy(FTPClient srcclient, FTPClient destclient) {
        try {
            srcclient.changeDirectory(srcBase);
//srcclient.changeDirectory(srcBase+"/modules/blockwishlist/mails/");
            destclient.changeDirectory(destBase);

            FTPFile[] filelist = null;
            try {
                filelist = srcclient.list();
            } catch (IllegalStateException ex) {
                ex.printStackTrace();
            } catch (IOException ex) {
                ex.printStackTrace();
            } catch (FTPIllegalReplyException ex) {
                ex.printStackTrace();
            } catch (FTPException ex) {
                ex.printStackTrace();
            } catch (FTPDataTransferException ex) {
                ex.printStackTrace();
            } catch (FTPAbortedException ex) {
                ex.printStackTrace();
            } catch (FTPListParseException ex) {
                ex.printStackTrace();
            }

            for (FTPFile file : filelist) {
                if(SkipList.skipfilters.contains(file.getName()))
                {
                    System.out.println("Skipping Directory "+srcclient.currentDirectory() + "/" +file.getName());
                    continue;
                }
                traverseit(srcclient, destclient, file);

            }
        } catch (IllegalStateException ex) {
            Logger.getLogger(Move.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(Move.class.getName()).log(Level.SEVERE, null, ex);
        } catch (FTPIllegalReplyException ex) {
            Logger.getLogger(Move.class.getName()).log(Level.SEVERE, null, ex);
        } catch (FTPException ex) {
            Logger.getLogger(Move.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    // Process all files and directories under dir
//    public static void visitAllDirsAndFiles(File dir) {
//        process(dir);
//
//        if (dir.isDirectory()) {
//            String[] children = dir.list();
//            for (int i = 0; i < children.length; i++) {
//                visitAllDirsAndFiles(new File(dir, children[i]));
//            }
//        }
//    }
    private static void traverseit(FTPClient srcclient, FTPClient destclient, FTPFile file) {
        try {
            //process it
            String filetype = file.getType() == 0 ? "File" : "Directory";
            System.out.println("Processing " + filetype + " " + srcclient.currentDirectory() + "/" + file.getName());
            if (FileFilter.filefilters.contains(file.getName()) || (srcclient.currentDirectory().contains("/mails/") && file.getType()==0)) {
                
                String srcfile = srcclient.currentDirectory();
                String destfile = srcfile.replace(srcBase, destBase);
                destfile = destfile.replace(srcAdmin, destAdmin);
                System.out.println("Moving File " + file.getName() + "from "+srcfile + "/" + file.getName()+" to "+ destfile + "/" + file.getName());
                try {
                    File tmpfile = new File(file.getName());
                    srcclient.download(srcclient.currentDirectory() + "/" + file.getName(), tmpfile);
                    destclient.changeDirectory(destfile);
                    destclient.upload(tmpfile);
                    tmpfile.delete();
                } catch (FileNotFoundException ex) {
                    ex.printStackTrace();
                } catch (FTPDataTransferException ex) {
                    ex.printStackTrace();
                } catch (FTPAbortedException ex) {
                    ex.printStackTrace();
                }
                
            }

            //IS DIRECTORY
            if (file.getType() == 1) {
                try {
                    srcclient.changeDirectory(srcclient.currentDirectory() + "/" + file.getName());
                    try {
                        FTPFile[] children = srcclient.list();
                        for (FTPFile child : children) {
                            traverseit(srcclient, destclient, child);
                        }
                        srcclient.changeDirectoryUp();
                    } catch (FTPDataTransferException ex) {
                        Logger.getLogger(Move.class.getName()).log(Level.SEVERE, null, ex);
                    } catch (FTPAbortedException ex) {
                        Logger.getLogger(Move.class.getName()).log(Level.SEVERE, null, ex);
                    } catch (FTPListParseException ex) {
                        Logger.getLogger(Move.class.getName()).log(Level.SEVERE, null, ex);
                    }


                } catch (IllegalStateException ex) {
                    ex.printStackTrace();
                } catch (IOException ex) {
                    ex.printStackTrace();
                } catch (FTPIllegalReplyException ex) {
                    ex.printStackTrace();
                } catch (FTPException ex) {
                    ex.printStackTrace();
                }
            }
        } catch (IllegalStateException ex) {
            Logger.getLogger(Move.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(Move.class.getName()).log(Level.SEVERE, null, ex);
        } catch (FTPIllegalReplyException ex) {
            Logger.getLogger(Move.class.getName()).log(Level.SEVERE, null, ex);
        } catch (FTPException ex) {
            Logger.getLogger(Move.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
